<?php
namespace Home\Controller\Login;
use Home\Controller\Layout\LayoutController;
class LoginController extends LayoutController {
	public function login(){
		if(session('home_id')){
			echo '<script>alert("您已登陆，无需重复登录");window.location.href="/"</script>';
			return ;
		}
		$this->display('/login/login/login');
	}
	public function code(){
		$Verify = new \Think\Verify();
		return $Verify->entry();
	}
	public function dologin(){
    	$m=M('user');
    	$res=$m->where('username="'.I('post.username').'"')->find();
    	// var_dump($res['password']);
    	if($res['password']==md5(I('post.password'))){
    		// echo 'yes';
    		session('home_id',$res['id']);
    		echo '<script>alert("登录成功");window.location.href="/"</script>';
    	}else{
    		echo '<script>alert("登录失败");window.location.href="/home/login/login/login"</script>';
    	}
	}
	public function yanzheng(){
		// var_dump(I('post.username'));
		$m=M('user');
		$res=$m->where('username="'.I('post.username').'"')->select();
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	public function yanzhengcode(){
		$code= I('post.code');                //这是提取页面上打字输入的code即验证码
        if(check_code($code)===false){       //给function.php中定义的函数check_code，然后它返回真假
        	// $this->error('验证码错误');
        	echo 'no';
        }else{
            // $this->error('验证码正确');
            echo 'yes';
        }
	}
	public function logout(){
		session('home_id',null);

		echo 'alert("注销成功");window.location.href="'.I('post.url').'"';
	}
	public function register(){
		if(session('home_id')){
			session('home_id',null);
		}
		$this->display('/login/login/register');
	}
	public function doregister(){
		$m=M('user');
		$m->username=I('post.username');
		$m->password=md5(I('post.password'));
		$m->status=0;
		$m->auto=1;
		$m->fenghao=0;
		$m->huishou=0;
		$res=$m->add();
		if($res){
			$p=M('personal');
			$p->uid=$res;
			$p->name=I('post.username');
			$p->pic='/public/admin/upload/images/1.jpg';
			$per=$p->add();
			if($per){
				echo '<script>alert("注册成功");window.location.href="/home/login/login/login"</script>';
			}else{
				echo '<script>alert("注册失败");window.location.href="/home/login/login/register"</script>';
			}
    	}else{
    		echo '<script>alert("注册失败");window.location.href="/home/login/login/register"</script>';
    	}
	}
	public function own(){
		if(session('home_id')){
			$m=M('personal');
			$res=$m->where('uid='.session('home_id'))->find();
			// var_dump($res);die;
			$this->assign('res',$res);
			$this->display('/login/login/own');
		}else{
			echo '<script>alert("您还未登陆,请登录后查看");window.location.href="/home/login/login/login"</script>';
		}
	}
	public function upload(){
		if($_FILES['pic']['name']!=''){
			$path=\Admin\Controller\Blog\ArticleController::uploadPic('/upload/','./public/home/touxiang');
			echo '<script type="text/javascript">
					//输出父窗口的变量
					parent.fun("'.$path.'");
				</script>';
		}
		$m=M('personal');
		$data=I('post.');
		$data['pic']=$path;
		$m->create($data);
		if($m->save()){
			echo '<script>alert("修改成功");window.location.href="/home/login/login/own"</script>';
		}else{
			echo '<script>alert("修改失败");window.location.href="/home/login/login/own"</script>';
		}
		
		
	}
	public function mima(){
		$m=M('user');
		$res=$m->where('id='.session('home_id'))->find();
		$this->assign('res',$res);
		$this->display('/login/login/mima');
	}
	public function domima(){
		// var_dump(I('post.'));
		$m=M('user');
		$res=$m->where('id='.session('home_id'))->setField('password',md5(I('post.password')));
		if($res){
			session('home_id',null);
			echo '<script>alert("修改密码成功,请重新登陆");window.location.href="/home/login/login/login"</script>';
		}else{
			echo '<script>alert("修改密码失败");window.location.href="/home/login/login/mima"</script>';
		}
	}
}
?>